package org.wu.framework.lazy.orm.core.persistence.conf.field;

import lombok.Data;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableIndexEndpoint;
import org.wu.framework.lazy.orm.core.persistence.util.LazySQLUtil;
import org.wu.framework.lazy.orm.core.source.adapter.SourceFactory;
import org.wu.framework.lazy.orm.core.stereotype.LazyFieldStrategy;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableField;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableFieldId;
import org.wu.framework.translation.data.IEnum;

import java.lang.reflect.Field;


@Data
public class DefaultLazyTableFieldEndpoint implements LazyTableFieldEndpoint {


    /**
     * 字段不是空
     *
     * @return
     */
    boolean notNull;
    /**
     * 字段默认数值
     */
    String defaultValue;
    /**
     * 字段是否为主键
     *
     * @return
     */
    boolean key;
    /**
     * 序号 数值越大越靠前
     */
    int serialNumber = 0;
    /**
     * Java 字段名称
     */
    private String name;
    /**
     * 数据库列名称
     */
    private String columnName;
    /**
     * 数据库字段别名
     */
    private String alias;
    /**
     * 数据库列类型
     */
    private String columnType;
    /**
     * 对应Java 字段类型class
     */
    private Class clazz;
    /**
     * 字段描述
     */
    private String comment;
    /**
     * 是否存在
     */
    private boolean exist;
    /**
     * upsert 时候字段策略，对应生成upsert 的sql
     */
    private LazyFieldStrategy upsertStrategy = LazyFieldStrategy.NO_VERIFY;

    /**
     * update 时候字段策略，对应生成upsert 的sql
     */
    private LazyFieldStrategy updateStrategy = LazyFieldStrategy.NO_VERIFY;
    /**
     * extra 额外的数据 如 auto_increment、DEFAULT_GENERATED on update CURRENT_TIMESTAMP
     *
     * @return
     */
    private String extra = "";
    /**
     * 特权 如 select,insert,update,references
     *
     * @return
     */
    private LazyTableField.Privilege[] privileges = {LazyTableField.Privilege.select, LazyTableField.Privilege.insert, LazyTableField.Privilege.update, LazyTableField.Privilege.references};
    /**
     * 字段对应索引
     */
    private LazyTableIndexEndpoint[] lazyTableIndexEndpoints = new LazyTableIndexEndpoint[0];
    /**
     * 字段版本
     */
    private int version;
    private long scale;
    /**
     * 参数
     */
    private String parameters;
    /**
     * 可选的
     */
    private boolean optional;
    /**
     * 数据为空的时候使用字段默认值
     */
    private String fieldDefaultValue;
    /**
     * 转换指定类型的枚举 DefaultIEnum 不转换  转换失败默认是-1
     */
    private Class<? extends IEnum> iEnum;
    /**
     * 数据格式 varchar、int、text
     */
    private String dataType;
    /**
     * 转换内容分隔符
     *
     * @return
     * @see IEnum#getConvertContentSeparator()
     */
    @Deprecated
    private String[] convertContentSeparator;
    /**
     * 通过获取字典类型转换
     *
     * @return
     */
    private String convert;
    /**
     * 通过 bean 产生的 AbstractLazyTableFieldEndpoint 字段数据
     */
    private Object fieldValue;
    /**
     * 字段
     */
    private Field field;
    private LazyTableFieldId.IdType idType;


    public String getAlias() {
        return SourceFactory.cleanSpecialColumn(alias);
    }


    @Override
    public String getColumnName() {
        return SourceFactory.cleanSpecialColumn(columnName);
    }


    /**
     * 获取 sql 字段对应的value 数据取自 getFieldValue
     *
     * @return sql value
     */
    @Override
    public Object getSqlValue() {
        return LazySQLUtil.valueToSqlValue(getFieldValue());
    }


    /**
     * @return 创建行sql
     * description ADD COLUMN `test_column12` varchar(255) NULL;
     * @author Jiawei Wu
     * @date 2020/12/31 9:19 下午
     **/
    @Override
    public String createColumnSQL() {
        return NormalUsedString.ADD + NormalUsedString.SPACE + NormalUsedString.COLUMN + NormalUsedString.SPACE + columnSQL() + NormalUsedString.SPACE;
    }

    /**
     * @return 行sql
     * description 创建SQL column
     * "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
     * @author Jiawei Wu
     * @date 2020/12/31 9:19 下午
     **/
    @Override
    public String columnSQL() {
        return getColumnName() + NormalUsedString.SPACE +
                getColumnType() + NormalUsedString.SPACE +
                (
                        isNotNull() ?
                                NormalUsedString.SPACE + NormalUsedString.NOT + NormalUsedString.SPACE + NormalUsedString.NULL + NormalUsedString.SPACE :
                                NormalUsedString.SPACE
                ) +
                (
                        ObjectUtils.isEmpty(getDefaultValue()) ?
                                NormalUsedString.SPACE :
                                NormalUsedString.SPACE + NormalUsedString.DEFAULT + NormalUsedString.SPACE + getDefaultValue() + NormalUsedString.SPACE) +
                NormalUsedString.SPACE + getExtra() + NormalUsedString.SPACE +
                NormalUsedString.COMMENT + NormalUsedString.SPACE +
                NormalUsedString.SINGLE_QUOTE + getComment() + NormalUsedString.SINGLE_QUOTE;
    }

    /**
     * 删除 行sql DROP COLUMN xx.`request_methods`
     *
     * @return DROP COLUMN xx.`request_methods`
     */
    @Override
    public String dropColumnSQL() {
        return NormalUsedString.DROP + NormalUsedString.SPACE + NormalUsedString.COLUMN + NormalUsedString.SPACE + getColumnName() + NormalUsedString.SPACE;
    }

    /**
     * MODIFY COLUMN `is_deleted` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否删除`;
     */
    @Override
    public String modifyColumnSQL() {
        return NormalUsedString.MODIFY + NormalUsedString.SPACE + NormalUsedString.COLUMN + NormalUsedString.SPACE + columnSQL() + NormalUsedString.SPACE;
    }

    @Override
    public LazyTableFieldEndpoint clone() throws CloneNotSupportedException {
        return (LazyTableFieldEndpoint) super.clone();
    }

    @Override
    public String toString() {
        return "[" +
                "name=" + name +
                ",columnName=" + columnName +
                ",fieldValue=" + fieldValue +
                ']';
    }
}
